package com.gzyj.flink.gps.service;

import com.bestvike.linq.Linq;
import com.gzyj.flink.driver.Driverlogininfo;
import com.gzyj.flink.dto.alarm.*;
import com.gzyj.flink.gps.GPSPosition;
import com.gzyj.flink.gps.utils.IntUtils;
import com.gzyj.flink.gps.utils.TimeUtils;

import java.util.Date;

public class NormalAlarmService {

    private YjGpsAddrService yjGpsAddrService;

    public NormalAlarmService() {
        yjGpsAddrService=new YjGpsAddrService();
    }

    public Kafka_VehicleRemindContent SendRemindToKafka(RemindContent remindContent) {
        Kafka_VehicleRemindContent content = new Kafka_VehicleRemindContent();
        content.setVehId(remindContent.getVehicleId());
        content.setVehNo(remindContent.getVehicleNo());
        content.setPlateColor(remindContent.getPlateColor());
        content.setRemindNote(remindContent.getRemindNote());
        content.setRemindTime(new Date());
        content.setRemindType(remindContent.getRemindType());
        content.setMessageType(remindContent.getMessageType());
        content.setAlarmNote(remindContent.getAlarmNote());
        content.setAlarmGuid(remindContent.getAlarmGuid());
        content.setGpsTime(remindContent.getGpsTime());
        content.setLastSpeed(remindContent.getLastSpeed());
        return content;
    }

    public Kafka_VehicleNormalAlarm SendAlarm(ProhibitDrivingData data, GPSPosition gps, boolean isAlarm, Driverlogininfo driverLoginData) {


        Kafka_VehicleNormalAlarm alarm = new Kafka_VehicleNormalAlarm();
        alarm.setVehicleId(gps.getVehId());
        alarm.setDeptId(gps.getDeptId());
        alarm.setNote("夜间禁行报警");
        alarm.setVelocity(gps.getSpeedExt());
        alarm.setAlarmBeginTime(TimeUtils.date2Str(data.getAlarmBeginTime()));
        alarm.setAlarmEndTime(TimeUtils.date2Str(data.getAlarmEndTime()));
        alarm.setAlarmTimeSpan(TimeUtils.getMinusSecond(data.getAlarmEndTime(), data.getAlarmBeginTime()));
        alarm.setMaxSpeed(data.getAlarmMaxSpeed());
        alarm.setDistance(data.getAlarmDistance() * 1000);
        alarm.setLimited(0);
        alarm.setLongitude(gps.getLngExt());
        alarm.setLatitude(gps.getLatExt());
        alarm.setElevation(Double.valueOf(gps.getALTITUDE()));
        alarm.setAngle(Integer.valueOf(gps.getDIRECTION()));
        alarm.setLocal(IntUtils.LocalExt(gps.getSTATUSBIT()) == 1);
        alarm.setAnalyzeModel(1);
        alarm.setCreateTime(TimeUtils.getNow());
        alarm.setUpdateTime(TimeUtils.getNow());
        alarm.setTime(gps.getTIME());
        alarm.setIsAlarm(isAlarm);
        alarm.setAlarmGuid(data.getSingleGuid());
        alarm.setCalcTime(TimeUtils.date2Str(data.getConditionBeginTime()));
        alarm.setDriver(driverLoginData.getDriverName());
        alarm.setDriverNo(driverLoginData.getDriverNo());
        alarm.setQualificationCode(driverLoginData.getDriverNo());
        alarm.setAlarmLevel(data.getAlarmLevel());
        alarm.setAlarmSetting(data.getAlarmSetting());
        alarm.setAcc(IntUtils.AccExt(gps.getSTATUSBIT()));
        alarm.setAltitude(Integer.valueOf(gps.getALTITUDE()));

        return alarm;


    }


    public Kafka_VehicleNormalAlarm SendAlarmToKafka(TotalFatigueDrivingData data, GPSPosition gps, Driverlogininfo driverLoginData, boolean isAlarm) {

        Kafka_VehicleNormalAlarm alarm = new Kafka_VehicleNormalAlarm();

        alarm.setVehicleId(gps.getVehId());
        alarm.setDeptId(gps.getDeptId());
        alarm.setNote("累计疲劳驾驶报警");
        alarm.setVelocity(gps.getSpeedExt());
        alarm.setAlarmBeginTime(TimeUtils.date2Str(data.getAlarmBeginTime()));
        alarm.setAlarmEndTime(TimeUtils.date2Str(data.getAlarmEndTime()));
        alarm.setAlarmTimeSpan(TimeUtils.getMinusSecond(data.getAlarmEndTime(), data.getAlarmBeginTime()));
        alarm.setMaxSpeed(data.getAlarmMaxSpeed());
        alarm.setDistance(data.getAlarmDistance() * 1000);
        alarm.setLimited(0);
        alarm.setLongitude(gps.getLngExt());
        alarm.setLatitude(gps.getLatExt());
        alarm.setElevation(Double.valueOf(gps.getALTITUDE()));
        alarm.setAngle(Integer.valueOf(gps.getDIRECTION()));
        alarm.setLocal(IntUtils.LocalExt(gps.getSTATUSBIT()) == 1);
        alarm.setAnalyzeModel(1);
        alarm.setCreateTime(TimeUtils.getNow());
        alarm.setUpdateTime(TimeUtils.getNow());
        alarm.setTime(gps.getTIME());
        alarm.setIsAlarm(isAlarm);
        alarm.setAlarmGuid(data.getSingleGuid());
        alarm.setCalcTime(TimeUtils.date2Str(data.getConditionBeginTime()));
        alarm.setDriver(driverLoginData.getDriverName());
        alarm.setDriverNo(driverLoginData.getDriverNo());
        alarm.setQualificationCode(driverLoginData.getDriverNo());
        alarm.setRemark("累计当日运行时间：" + Linq.of(data.getOnceDayTotalRunTime()).firstOrDefault().getValue());

        return alarm;

    }

    public Kafka_VehicleNormalAlarm SendAlarmToKafka(NoPositionData data, GPSPosition gps, Driverlogininfo driverLoginData, boolean isAlarm) {

        Kafka_VehicleNormalAlarm alarm = new Kafka_VehicleNormalAlarm();
        alarm.setVehicleId(gps.getVehId());
        alarm.setDeptId(gps.getDeptId());
        alarm.setNote("无数据报警");
        alarm.setVelocity(gps.getSpeedExt());
        alarm.setAlarmBeginTime(TimeUtils.date2Str(data.getAlarmBeginTime()));
        alarm.setAlarmEndTime(TimeUtils.date2Str(data.getAlarmEndTime()));
        alarm.setAlarmTimeSpan(TimeUtils.getMinusSecond(data.getAlarmEndTime(), data.getAlarmBeginTime()));
        alarm.setMaxSpeed(data.getAlarmMaxSpeed());
        alarm.setDistance(data.getAlarmDistance() * 1000);
        alarm.setLimited(0);
        alarm.setLongitude(gps.getLngExt());
        alarm.setLatitude(gps.getLatExt());
        alarm.setElevation(Double.valueOf(gps.getALTITUDE()));
        alarm.setAngle(Integer.valueOf(gps.getDIRECTION()));
        alarm.setLocal(IntUtils.LocalExt(gps.getSTATUSBIT()) == 1);
        alarm.setAnalyzeModel(1);
        alarm.setCreateTime(TimeUtils.getNow());
        alarm.setUpdateTime(TimeUtils.getNow());
        alarm.setTime(gps.getTIME());
        alarm.setIsAlarm(isAlarm);
        alarm.setAlarmGuid(data.getSingleGuid());
        alarm.setCalcTime(TimeUtils.date2Str(data.getConditionBeginTime()));
        alarm.setDriver(driverLoginData.getDriverName());
        alarm.setDriverNo(driverLoginData.getDriverNo());
        alarm.setQualificationCode(driverLoginData.getDriverNo());
        alarm.setAcc(IntUtils.AccExt(gps.getSTATUSBIT()));
        alarm.setAltitude(gps.getALTITUDE());
        return alarm;

    }

    public Kafka_VehicleNormalAlarm SendAlarmToKafka(NoLocateData data, GPSPosition gps, Driverlogininfo driverLoginData, boolean isAlarm) {
        Kafka_VehicleNormalAlarm alarm = new Kafka_VehicleNormalAlarm();
        alarm.setVehicleId(gps.getVehId());
        alarm.setDeptId(gps.getDeptId());
        alarm.setNote("不定位报警");
        alarm.setVelocity(gps.getSpeedExt());
        alarm.setAlarmBeginTime(TimeUtils.date2Str(data.getAlarmBeginTime()));
        alarm.setAlarmEndTime(TimeUtils.date2Str(data.getAlarmEndTime()));
        alarm.setAlarmTimeSpan(TimeUtils.getMinusSecond(data.getAlarmEndTime(), data.getAlarmBeginTime()));
        alarm.setMaxSpeed(data.getAlarmMaxSpeed());
        alarm.setDistance(data.getAlarmDistance() * 1000);
        alarm.setLimited(0);
        alarm.setLongitude(gps.getLngExt());
        alarm.setLatitude(gps.getLatExt());
        alarm.setElevation(Double.valueOf(gps.getALTITUDE()));
        alarm.setAngle(Integer.valueOf(gps.getDIRECTION()));
        alarm.setLocal(IntUtils.LocalExt(gps.getSTATUSBIT()) == 1);
        alarm.setAnalyzeModel(1);
        alarm.setCreateTime(TimeUtils.getNow());
        alarm.setUpdateTime(TimeUtils.getNow());
        alarm.setTime(gps.getTIME());
        alarm.setIsAlarm(isAlarm);
        alarm.setAlarmGuid(data.getSingleGuid());
        alarm.setCalcTime(TimeUtils.date2Str(data.getConditionBeginTime()));
        alarm.setDriver(driverLoginData.getDriverName());
        alarm.setDriverNo(driverLoginData.getDriverNo());
        alarm.setQualificationCode(driverLoginData.getDriverNo());
        alarm.setAcc(IntUtils.AccExt(gps.getSTATUSBIT()));
        alarm.setAltitude(gps.getALTITUDE());
        return alarm;
    }

    public Kafka_VehicleNormalAlarm SendAlarmToKafka(AbnormalOfflineData data, GPSPosition gps, Driverlogininfo driverLoginData, boolean isAlarm) {
        Kafka_VehicleNormalAlarm alarm = new Kafka_VehicleNormalAlarm();
        alarm.setVehicleId(gps.getVehId());
        alarm.setDeptId(gps.getDeptId());
        alarm.setNote("异常掉线报警");
        alarm.setVelocity(gps.getSpeedExt());
        alarm.setAlarmBeginTime(TimeUtils.date2Str(data.getAlarmBeginTime()));
        alarm.setAlarmEndTime(TimeUtils.date2Str(data.getAlarmEndTime()));
        alarm.setAlarmTimeSpan(TimeUtils.getMinusSecond(data.getAlarmEndTime(), data.getAlarmBeginTime()));
        alarm.setMaxSpeed(data.getAlarmMaxSpeed());
        alarm.setDistance(data.getAlarmDistance() * 1000);
        alarm.setLimited(0);
        alarm.setLongitude(gps.getLngExt());
        alarm.setLatitude(gps.getLatExt());
        alarm.setElevation(Double.valueOf(gps.getALTITUDE()));
        alarm.setAngle(Integer.valueOf(gps.getDIRECTION()));
        alarm.setLocal(IntUtils.LocalExt(gps.getSTATUSBIT()) == 1);
        alarm.setAnalyzeModel(1);
        alarm.setCreateTime(TimeUtils.getNow());
        alarm.setUpdateTime(TimeUtils.getNow());
        alarm.setTime(gps.getTIME());
        alarm.setIsAlarm(isAlarm);
        alarm.setAlarmGuid(data.getSingleGuid());
        alarm.setCalcTime(TimeUtils.date2Str(data.getConditionBeginTime()));
        alarm.setDriver(driverLoginData.getDriverName());
        alarm.setDriverNo(driverLoginData.getDriverNo());
        alarm.setQualificationCode(driverLoginData.getDriverNo());
        alarm.setAcc(IntUtils.AccExt(gps.getSTATUSBIT()));
        alarm.setAltitude(gps.getALTITUDE());
        return alarm;
    }

    public Kafka_VehicleAreaAlarm SendAlarm(AreaData data, GPSPosition gps,Driverlogininfo driverlogininfo) {


        String note = data.InArea ? "进区域报警" : "出区域报警";
        Kafka_VehicleAreaAlarm alarm = new Kafka_VehicleAreaAlarm();
        alarm.setVehicleId(gps.getVehId());
        alarm.setNote(note);
        alarm.setVelocity(gps.getSpeedExt());
        alarm.setAreaId(data.getAreaId());
        alarm.setAreaName(data.getAreaName());
        alarm.setTimes(data.getKeepTimes());
        alarm.setIsAcross(data.isInArea());
        alarm.setTime(gps.getGPSTimeExt());
        alarm.setCreateTime(new Date());
        alarm.setAlarmGuid(data.getAlarmGuid());
        alarm.setLongitude(data.getLongitude());
        alarm.setLatitude(data.getLatitude());
        alarm.setRoadName(yjGpsAddrService.getAddress(data.Latitude, data.Longitude));
        alarm.setDriver(driverlogininfo.getDriverName());
        alarm.setDriverNo(driverlogininfo.getDriverNo());
        alarm.setQualificationCode(driverlogininfo.getDriverNo());



        return alarm;


    }
}
